﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
using System.Net;
using WomanMotion.WebApi.Api.Utility;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Infrastructure.UnifyResult;
using WomanMotion.WebApi.Model.Dtos;
using WomanMotion.WebApi.Model.Dtos.Carousel;
using WomanMotion.WebApi.Model.Dtos.Classify;
using WomanMotion.WebApi.Model.Entitys;
using WomanMotion.WebApi.Service;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Api.Controllers
{
    /// <summary>
    /// 轮播图管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [XSSFilter]
    [ApiController]
    [DescriptionName("轮播图管理", true, 100)]
    public class CarouselController : BaseController
    {
        private readonly ICarouselService _carousel;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="carousel"></param>
        public CarouselController(ILogger<CarouselController> logger, ICarouselService carousel) : base(logger)
        {
            _carousel = carousel;
        }

        /// <summary>
        /// 创建轮播图
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<CarouselVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("创建轮播图", true, "轮播图管理", "List", "CarouseList")]
        public async Task<IResultOutput> Created(CreatedCarouselDTO dto)
        {
            var res = await _carousel.CreatedAsync(dto);
            if (res != null)
            {
                return ResultOutput.Ok(res, "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 启用轮播图
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("启用轮播图")]
        public async Task<IResultOutput> Enable(PrimaryKeyDTO ids)
        {
            var res = await _carousel.UpdateDisableAsync(DisableEnum.Enable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 禁用轮播图
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("禁用轮播图")]
        public async Task<IResultOutput> Disable(PrimaryKeyDTO ids)
        {
            var res = await _carousel.UpdateDisableAsync(DisableEnum.Disable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 更新轮播图
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<CarouselVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("更新轮播图")]
        public async Task<IResultOutput> Update(UpdateCarouselDTO dto)
        {
            var res = await _carousel.UpdateAsync(dto);
            if (res == null)
            {
                return ResultOutput.NotOk("操作失败");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取轮播图数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [ProducesResponseType(typeof(SwaggerResultTemplate<CarouselVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取轮播图数据")]
        public async Task<IResultOutput> GetById(long id)
        {
            var res = await _carousel.GetByIdAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取多表连接轮播图信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [ProducesResponseType(typeof(SwaggerResultTemplate<CarouselVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取多表连接轮播图信息")]
        public async Task<IResultOutput> GetCommonById(long id)
        {
            var res = await _carousel.GetByIDNavAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 分页获取分类数据
        /// </summary>
        /// <param name="pars">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<PageViewBase<List<CarouselVM>>>), (int)HttpStatusCode.OK)]
        [DescriptionName("分页获取轮播图信息数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetPageList(QueryParameterBase pars)
        {
            var res = await _carousel.GetPageNavListAsync(pars);
            if (res.Data == null || res.Data.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 获取所有轮播图信息数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<List<CarouselVM>>), (int)HttpStatusCode.OK)]
        [DescriptionName("获取所有轮播图信息数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetList()
        {
            var res = await _carousel.GetListNavAsync(m => m.Ifdisable == 1);
            if (res == null || res.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }
    }
}